package com.project.market.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.project.market.db.domain.MarketAddress;
import com.project.market.db.domain.MarketAdmin;
import com.project.market.db.domain.MarketOrder;
import com.project.market.service.AdminOrderService;
import com.project.market.service.impl.AdminOrderServiceImpl;
import com.project.market.util.JacksonUtil;
import com.project.market.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/admin/order/*")
public class AdminOrderController extends HttpServlet {

    AdminOrderService adminOrderService = new AdminOrderServiceImpl();


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String op = req.getRequestURI().replace(req.getContextPath() + "/admin/order/", "");
        if ("delete".equals(op)) {
            delete(req, resp);
        } else if ("ship".equals(op)) {
            ship(req, resp);
        } else if ("refund".equals(op)) {
            refund(req, resp);
        }
    }

    private void refund(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        BufferedReader reader = req.getReader();
        String s = reader.readLine();
        Integer orderId = JacksonUtil.parseInteger(s, "orderId");
        String refundMoney = JacksonUtil.parseString(s, "refundMoney");

        Integer index = adminOrderService.refund(orderId, refundMoney);
        if (index == 1) {
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok()));
        } else {
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.fail()));
        }
    }

    private void ship(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        BufferedReader reader = req.getReader();
        String s = reader.readLine();
        Integer orderId = JacksonUtil.parseInteger(s, "orderId");
        String shipChannel = JacksonUtil.parseString(s, "shipChannel");
        String shipSn = JacksonUtil.parseString(s, "shipSn");

        Integer index = adminOrderService.ship(orderId, shipChannel, shipSn);
        if (index == 1) {
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok()));
        } else {
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.fail()));
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        BufferedReader reader = req.getReader();
        String s = reader.readLine();
        Integer id = JacksonUtil.parseInteger(s, "orderId");
        Integer index = adminOrderService.delete(id);
        if (index == 1) {
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok()));
        } else {
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.fail()));
        }
    }


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String op = req.getRequestURI().replace(req.getContextPath() + "/admin/order/", "");
        if ("list".equals(op)) {
            list(req, resp);
        } else if ("detail".equals(op)) {
            detail(req, resp);
        } else if ("channel".equals(op)) {
            channel(req, resp);
        }
    }

    private void channel(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<Object> list = adminOrderService.channel();
        resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok(list)));
    }

    private void detail(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        HttpSession session = req.getSession();
        MarketAdmin admin = (MarketAdmin) session.getAttribute("admin");
        System.out.println(admin);
        Map<String, Object> user = new HashMap<>();
        user.put("nickname", admin.getUsername());
        user.put("avatar", admin.getAvatar());
        Map<String, Object> map = adminOrderService.detail(id);
        if (map == null) {
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.fail(402, "参数值错误")));
        } else {
            map.put("user", user);
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.ok(map)));
        }
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 获取前端传递的参数
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String start = req.getParameter("start");
        String end = req.getParameter("end");

        String timeArray = req.getParameter("timeArray");

        // 订单状态
        String orderStatusArray = req.getParameter("orderStatusArray");

        // 用户ID
        String userId = req.getParameter("userId");
        // 订单编号
        String orderSn = req.getParameter("orderSn");

        List<MarketOrder> list = adminOrderService.list(page, limit, sort, order, start, end, userId, orderSn, orderStatusArray);

        if (list == null) {
            resp.getWriter().println(JacksonUtil.writeValueAsString(ResponseUtil.fail(402, "参数值错误")));
        } else {
            Object ok = ResponseUtil.okList(list);
            String responseBody = JacksonUtil.writeValueAsString(ok);
            resp.getWriter().println(responseBody);
        }
    }
}
